package net.sevear.chaos.douban
{
	import com.douban.auth.DBAuthenticate;
	
	import de.popforge.utils.SprintfUtil;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	import net.sevear.chaos.utils.Logger;
	import net.sevear.chaos.vo.TrackInfo;
	
	public class SendMailTool extends EventDispatcher
	{
		private static const SEND_MAIL_URL:String = "http://www.douban.com/doumail/write";
		private static const READY_TO_WRITE_URI:String = "?to=%d";
		
		private static const RECOMMEND_MAIL_TITLE:String = "%s,我通过Douchaos推荐歌曲给你啦";
		
		private static const STEP1:int = 0;
		private static const STEP2:int = 1;
		
		private var loader:URLLoader;
		private var waitingList:Array;
		private var isSendingRequest:Boolean = false;
		private var curObj:Object = null;
		private var step:int = STEP1;
		
		public function SendMailTool()
		{
			super(null);
			loader = new URLLoader();
			loader.addEventListener(Event.COMPLETE,onComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR,onIoErr);
			waitingList = [];
		}
		
		public function getRecommendContent(writeToID:int,writeToName:String,track:TrackInfo):String{
			return SprintfUtil.sprintf("%s,\n\t\t,我从DouChaos推荐了一首%s-%s给你听，你打开Douchaos就可以听得啦"
				,writeToName,track.artistName,track.name);
		}
		
		public function getRecommendTitle(writeToID:int,writeToName:String,track:TrackInfo):String{
			return SprintfUtil.sprintf(RECOMMEND_MAIL_TITLE,writeToName);	
		}
		
		public function addSendMail(writeToID:int,title:String,content:String):void{
			if(isSendingRequest==false){
				step = STEP1;
				curObj = {toID:writeToID,title:String,content:content};
				loader.load(new URLRequest(SprintfUtil.sprintf(SEND_MAIL_URL+READY_TO_WRITE_URI,writeToID)));
				isSendingRequest = true;
			}else{
				waitingList.push({toID:writeToID,title:String,content:content});
			}
		}
		
		private function onComplete(e:Event):void{
			switch(step){
				case STEP1:
					postMail(curObj.toID as int,curObj.title as String,curObj.content as String);
					break;
				case STEP2:
					isSendingRequest = false;
					Logger.getInstance().logInfo(SprintfUtil.sprintf("Send recommending DouBan mail to %d sucessfully",curObj.toID));
					if(waitingList.length>0){
						var obj:Object = waitingList.shift() as Object;
						addSendMail(obj.toID as int,obj.title as String,obj.content as String);
					}
					break;
			}
		}
		
		private function postMail(writeToID:int,title:String,content:String):void{
			step = STEP2;
			var rst:URLRequest = new URLRequest(SEND_MAIL_URL);
			var vars:URLVariables = new URLVariables();
			vars.ck = DBAuthenticate.getInstance().ck;
			vars.to = writeToID.toString();
			vars.m_title = title;
			vars.m_text = content;
			//FIXME:
			vars.m_submit = "好了，寄出去";
			rst.data = vars;
			rst.manageCookies = true;
			rst.followRedirects = true;
			rst.method = URLRequestMethod.POST;
			loader.load(rst);
		}
		
		private function onIoErr(e:IOErrorEvent):void{
			isSendingRequest = false;
			Logger.getInstance().logInfo(SprintfUtil.sprintf("IO Error: Send recommending DouBan mail to %d failed",curObj.toID));
			if(waitingList.length>0){
				var obj:Object = waitingList.shift() as Object;
				addSendMail(obj.toID as int,obj.title as String,obj.content as String);
			}
		}
	}
}